package com.yihusitian.zsxq.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yihusitian.zsxq.pojo.data.UserDO;
import com.yihusitian.zsxq.pojo.dto.Answer;
import com.yihusitian.zsxq.service.IDGenerator;
import com.yihusitian.zsxq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yihusitian.zsxq.pojo.data.AnswerDO;
import com.yihusitian.zsxq.service.AnswerService;
import com.yihusitian.zsxq.mapper.AnswerMapper;

/**
 * @Description TODO
 * @Author LeeHo
 * @Date 2021-03-25 11:49:27
 */
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, AnswerDO> implements AnswerService {

    @Autowired
    private UserService userService;

    @Override
    public AnswerDO createAnswer(Answer answer, Long topicId) {
        if (answer == null) {
            return null;
        }
        UserDO userDO = userService.createOrUpdate(answer.getOwner());
        AnswerDO answerDO = AnswerDO.builder().id(IDGenerator.generateId()).ownerId(userDO.getId()).text(StrUtil.emptyToDefault(answer.getText(), StrUtil.EMPTY)).topicId(topicId).build();
        answerDO.insert();
        return answerDO;
    }
}